import React, { Component } from 'react'
import {ListBox} from '../../../components'
import UpdateWorkareaPage from './UpdateNew'
import { fetchData } from '../../../components/AjaxConsumer'
import { Modal } from 'antd';

export default class ListWorkareaPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableColumn: {},
            searchComponentData: []
        };
        //行操作列表
        this.rowOperation = [
            {
                handleName: 'edit',
                iconClass: 'cellEdit'
            }, {
                handleName: 'delete',
                iconClass: 'cellDele'
            }, {
                handleName: 'assignUser',
                iconClass: 'cellRole'
            }
        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'addWorkarea',
                showName: '新增',
                iconClass: 'plus'
            },{
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit'
            }
            ,{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            }
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'listWorkareaColumnConfig',
            data: [
                {
                    showName: 'workareaId',
                    columnName: 'workareaId',
                    columnHidden:true,
                    foreverHidden: true
                },
                {
                    showName: '工作区编码',
                    columnName: 'workareaCode',
                    columnWidth: 130
                }, 
                {
                    showName: '工作区名称',
                    columnName: 'workareaName'
                },
                {
                    showName: '仓库编码',
                    columnName: 'whCode'
                },
                {
                    showName: '区域编码',
                    columnName: 'regionCode'
                }
/*                , {
                    showName: '工厂编码',
                    columnName: 'regionCode'
                },
                {
                    showName: '工厂名称',
                    columnName: 'regionCode'
                }*/
                , {
                    showName: '激活标记',
                    columnName: 'activeFlag',
                    showOption:[
                        {
                            optionKey:'1',
                            optionShow:'启用'
                        },{
                            optionKey:'0',
                            optionShow:'禁用'
                        }
                    ]
                }, {
                    showName: '创建人',
                    columnName: 'createBy'
                }, {
                    showName: '创建时间',
                    columnName: 'gmtCreate',
                    columnWidth: 130
                }, {
                    showName: '最后修改人',
                    columnName: 'lastModifiedBy'
                }, {
                    showName: '最后修改时间',
                    columnName: 'gmtModified',
                    columnWidth: 130
                }
            ]
        };

        //搜索属性配置
        this.searchComponentData = [
            {
                name: 'activeFlag',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },
                    {
                        optionKey:'1',
                        optionShow:'启用'
                    },{
                        optionKey:'0',
                        optionShow:'禁用'
                    }
                ]
            },{
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax',
                minName:'gmtCreateMin'
            },{
                name:'gmtModified',
                type: 'date',
                maxName:'gmtModifiedMax',
                minName:'gmtModifiedMin'
            }
        ];

    }

    rowHandles = (strHandles, rowObj) => {
        console.log("strHandles===ListResource=======", strHandles);
        if (strHandles === 'edit') {
            this.editHandle(rowObj);
        } else if (strHandles === 'delete') {
            this.deleteHandle(rowObj);
        } else if (strHandles === 'addWorkarea') {
            this.addWorkareaHandle();
        } else if (strHandles == 'assignUser') {
            this.assignFactoryUserHandle(rowObj);
        }
    }
    // 增加工作区
    addWorkareaHandle = () => {
        console.log("====执行了新增工作区操作====");
        this.refs.updateWorkarea.addWorkareaHandle();
    }
    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updateWorkarea.editHandle(rowObj[0].data);
        }
    }

    deleteHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updateWorkarea.deleteHandle(rowObj[0].data)
        }
    }
    // 给区域分配角色
    assignFactoryUserHandle = (rowObj) => {
        let workareaId = rowObj.workareaId;
        this.refs.assignFactoryUser.initModal(workareaId);
    }


    //刷新列表
    refreshHandle = () => {
        this.refs.listWorkarea.refreshHandle();
    }


    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return (
            <div>
                <ListBox
                    ref='listWorkarea'
                    title="工作区管理"
                    isHideLeft={isHideLeft}
                    tableColumnData={this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url='/base/workarea/list'
                    rowHandles={this.rowHandles}
                    rowOperation={this.rowOperation}
                    headerOperation={this.headerOperation}
                    isRowSingle = "true"
                />

                <UpdateWorkareaPage
                    ref="updateWorkarea"
                    refreshHandle={this.refreshHandle}
                />

            </div>
        )
    }
}
